local zhanshe = fk.CreateSkill {
  name = "hanqing__zhanshe",
}

Fk:loadTranslationTable{
  ["hanqing__zhanshe"] = "斩蛇",
  [":hanqing__zhanshe"] = "出牌阶段限一次，你可以将一张牌当【杀】使用或将一张【杀】当【过河拆桥】使用。"..
  "结算后，若目标手牌数因此变为偶数，其弃置一半手牌；若其因此失去点数为7的牌，其失去体力至1点。",

  ["#hanqing__zhanshe"] = "斩蛇：你可以将一张牌当【杀】、【杀】当【过河拆桥】使用",
}

zhanshe:addEffect("viewas", {
  prompt = "#hanqing__zhanshe",
  interaction = function(self, player)
    return UI.CardNameBox {choices = {"slash", "dismantlement"}}
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      if self.interaction.data == "slash" then
        return true
      elseif self.interaction.data == "dismantlement" then
        return Fk:getCardById(to_select).trueName == "slash"
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = zhanshe.name
    return card
  end,
  before_use = function (self, player, use)
    use.extra_data = {}
    use.extra_data.hanqing__zhanshe1 = {}
    for _, p in ipairs(use.tos) do
      if p:getHandcardNum() % 2 == 1 then
        table.insertIfNeed(use.extra_data.hanqing__zhanshe1, p)
      end
    end
  end,
  after_use = function (self, player, use)
    local room = player.room
    use.extra_data.hanqing__zhanshe1 = use.extra_data.hanqing__zhanshe1 or {}
    use.extra_data.hanqing__zhanshe2 = use.extra_data.hanqing__zhanshe2 or {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.dead then
        if table.contains(use.extra_data.hanqing__zhanshe1, p) and not p:isKongcheng() and p:getHandcardNum() % 2 == 0 then
          local n = p:getHandcardNum() // 2
          local cards = room:askToDiscard(p, {
            min_num = n,
            max_num = n,
            include_equip = false,
            skill_name = zhanshe.name,
            cancelable = false,
          })
          if table.find(cards, function (id)
            return Fk:getCardById(id).number == 7
          end) then
            table.insertIfNeed(use.extra_data.hanqing__zhanshe2, p)
          end
        end
        if not p.dead and table.contains(use.extra_data.hanqing__zhanshe2, p) and p.hp > 1 then
          room:loseHp(p, p.hp - 1, zhanshe.name)
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(zhanshe.name, Player.HistoryPhase) == 0
  end,
})

zhanshe:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player.seat == 1
  end,
  on_refresh = function (self, event, target, player, data)
    local card_effect = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
    if card_effect and table.contains(card_effect.data.card.skillNames, zhanshe.name) then
      local use_event = card_effect:findParent(GameEvent.UseCard)
      if use_event then
        local use = use_event.data
        if use.card == card_effect.data.card then
          use.extra_data = use.extra_data or {}
          use.extra_data.hanqing__zhanshe2 = use.extra_data.hanqing__zhanshe2 or {}
          for _, move in ipairs(data) do
            if table.contains(use.tos, move.from) then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId).number == 7 then
                  table.insertIfNeed(use.extra_data.hanqing__zhanshe2, move.from)
                end
              end
            end
          end
        end
      end
    end
  end,
})

zhanshe:addAI(nil, "vs_skill")

return zhanshe
